﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using JumbotCms.Common;
using JumbotCms.API;
namespace JumbotCms.WebFile.Controls
{
    public partial class _class : JumbotCms.API.FrontHtml
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Server.ScriptTimeout = 8;//脚本过期时间
            int CurrentPage = Int_ThisPage();
            string ClassId = (this.lblClassId.Text == "{$ClassId}") ? Str2Str(q("id")) : Str2Str(this.lblClassId.Text);
            string ChannelId = (this.lblChannelId.Text == "{$ChannelId}") ? Str2Str(q("ChannelId")) : Str2Str(this.lblChannelId.Text);

            doh.Reset();
            if (q("preview") == "1")
                doh.ConditionExpress = "id=@id";
            else
                doh.ConditionExpress = "id=@id and Enabled=1";
            doh.AddConditionParameter("@id", ChannelId);
            if (!doh.Exist("jcms_normal_channel"))
            {
                FinalMessage("频道不存在或被禁用!", site.Dir, 0, 8);
                Response.End();
            }
            doh.Reset();
            doh.SqlCmd = "SELECT ID FROM [jcms_normal_class] WHERE [IsOut]=0 AND [ChannelId]=" + ChannelId + " and [Id]=" + ClassId;
            DataTable dtSearch = doh.GetDataTable();
            if (dtSearch.Rows.Count == 0)
            {
                FinalMessage("栏目不存在或已被删除!", site.Dir, 0, 8);
                Response.End();
            }
            dtSearch.Clear();
            dtSearch.Dispose();
            TemplateEngine te = new TemplateEngine(doh, ChannelId);
            DataPageCount dtp = new DataPageCount(doh, ChannelId);
            int pageCount = dtp.Class(ClassId, true);
            CurrentPage = Jumbot.Common.Int.Min(CurrentPage, pageCount);
            if (!te.PageIsHtml() || CurrentPage > site.CreatePages)//直接动态
            {
                te.IsHtml = false;
                string TxtStr = te.GetClassStr(ClassId, CurrentPage);
                Response.Write(TxtStr);//直接输出
            }
            else
            {
                te.IsHtml = true;
                string HtmlUrl = Go2Class(doh, CurrentPage, true, ChannelId, ClassId);
                if (!System.IO.File.Exists(Server.MapPath(HtmlUrl)))//保存静态
                {
                    string TxtStr = te.GetClassStr(ClassId, CurrentPage);
                    SaveCacheFile(TxtStr, Server.MapPath(HtmlUrl));
                }
                Response.Redirect(HtmlUrl);
            }
        }
    }
}
